package com.awesome.spring.service.impl;

import com.awesome.spring.bean.Order;
import com.awesome.spring.mapper.OrderMapper;
import com.awesome.spring.service.OrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * OrderServiceImpl
 *
 * @author alex wu
 * @date 2023/10/23
 */
@Service("orderService")
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Override
    public boolean create(String orderCode, String userId, int money, int count, boolean rollback) {
        System.out.println("order下单");

        Order order = new Order();
        order.setCode(orderCode);
        order.setUserId(userId);
        order.setMoney(money);
        order.setCount(count);
        this.save(order);

//        if (rollback) {
//            int a = 1/0;
//        }

        return true;
    }

    @Override
    public boolean compensateCreate(String orderCode, String userId, int money) {
        System.out.println("order下单补偿");
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("code", orderCode);
        this.remove(queryWrapper);

        return true;
    }
}
